package com.level2app.ui.search;

import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.level2app.adapter.SearchResultAdapter;
import com.level2app.base.BaseFragment;
import com.level2app.databinding.FragmentSearchResultBinding;
import com.level2app.ui.search.view.SearchResultFrgViewModel;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by S
 * time 2024/8/21
 */
public class SearchResultFragment extends BaseFragment<FragmentSearchResultBinding, SearchResultFrgViewModel> {

    private int type;
    private SearchResultAdapter adapter;

    public static SearchResultFragment newInstance(int type) {
        SearchResultFragment searchResultFragment = new SearchResultFragment();
        Bundle args = new Bundle();
        args.putInt("type", type);
        searchResultFragment.setArguments(args);
        return searchResultFragment;
    }

    @Override
    protected SearchResultFrgViewModel bindModel() {
        return getViewModel(this, SearchResultFrgViewModel.class);
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    protected void initData() {
        type = getArguments().getInt("type");
        binding.refreshLayout.setRefreshHeader(new ClassicsHeader(getContext()));
        binding.refreshLayout.setRefreshFooter(new ClassicsFooter(getContext()));
        adapter = new SearchResultAdapter();
//        binding.recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
        //瀑布流
        binding.recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        binding.recyclerView.setAdapter(adapter);

        if (type == 1) {
            binding.emptyRL.setVisibility(View.GONE);
            binding.recyclerView.setVisibility(View.VISIBLE);
            List<String> data = new ArrayList<>();
            data.add("111");
            data.add("122221123");
            data.add("333");
            data.add("444");
            data.add("555");
            adapter.submitList(data);
        } else {
            binding.emptyRL.setVisibility(View.VISIBLE);
            binding.recyclerView.setVisibility(View.GONE);
//            binding.recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 1));
//            adapter.setEmptyViewLayout(getContext(), R.layout.categories_offer_empty_view);
//            adapter.setEmptyViewEnable(true);
            binding.refreshLayout.setEnableRefresh(false);
            binding.refreshLayout.setEnableLoadMore(false);
        }
    }

    @Override
    protected void initClick() {
        super.initClick();
        binding.refreshLayout.setOnRefreshListener(refreshLayout -> {
            //刷新
            binding.refreshLayout.finishRefresh();
        });
        binding.refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            //加载更多
            binding.refreshLayout.finishLoadMore();
        });
        adapter.setOnItemClickListener((baseQuickAdapter, view, i) -> {

        });
    }
}